$(function(){

   /* $("#idTerm").change(function () {
        var termId=$("#idTerm").val();
        if(termId==-1){
            return;
        }

        var map = '{"lTermId":"' + termId + '"}';
        $.ajax({
            cache:true,
            type:"post",
            url:"/class/getByCondition",
            dataType : 'json',
            data:map,
            contentType: "application/json;charset=UTF-8",
            success:function (data) {
                //console.log(data);
                var idClass=$("#idClass").empty();
                if(data.length==0){
                    idClass.append("<option value=-1>无数据</option>");
                }
                for (var i=0;i<data.length;i++){
                    idClass.append("<option value='"+data[i].lid+"'>"+data[i].strclassname+"</option>");
                    if (i==0){
                        $.ajax({
                            cache:true,
                            type:"post",
                            url:"/test/getByCondition",
                            dataType : 'json',
                            data:'{"lClassId":"' + data[i].lid + '"}',
                            contentType: "application/json;charset=UTF-8",
                            success:function (data) {
                                var idClass=$("#idTest").empty();
                                if(data.length==0){
                                    idClass.append("<option value=-1>无数据</option>");
                                }
                                for (var i=0;i<data.length;i++){
                                    idClass.append("<option value='"+data[i].lid+"'>"+data[i].strtestname+"</option>");

                                }

                            },error:function (error) {
                                alert("数据加载失败，请重新刷新!");
                            }
                        });
                    }

                }

            },error:function (error) {
                alert("数据加载失败，请重新刷新:"+error);
            }
        });

    });*/


/*    $("#idClass").change(function () {
        var classId=$("#idClass").val();
        if(classId==-1){
            return;
        }

        var map = '{"lClassId":"' + classId + '"}';
        $.ajax({
            cache:true,
            type:"post",
            url:"/class/getByCondition",
            dataType : 'json',
            data:map,
            contentType: "application/json;charset=UTF-8",
            success:function (data) {
                //console.log(data);
                var idClass=$("#idTest").empty();
                if(data.length==0){
                    idClass.append("<option value=-1>无数据</option>");
                }
                for (var i=0;i<data.length;i++){
                    idClass.append("<option value='"+data[i].ntimes+"'>"+data[i].strtestname+"</option>");

                }

            },error:function (error) {
                alert("数据加载失败，请重新刷新!");
            }
        });
    });*/




    $("#search").click(function(){
        var map="{";
        var count=0;
        var title="";
        $("select option:selected").each(function () {
            title=title+$(this).text();
            map=map+'"'+$(this).parent().attr("name")+'":"' + $(this).val() + '",';
            count=count+1;
        })
        map=map.substr(0,map.length-1);
        map=map+"}";
        if(count<4){
            alert("所有条件必须选取！");
            return;
        }
        $.ajax({
            cache:true,
            type:"post",
            url:"/score/getByCondition",
            dataType : 'json',
            data:map,
            contentType: "application/json;charset=UTF-8",
            success:function (data) {
                var names=[];
                var scores=[];
                for (var i=0;i<data.length;i++){
                    names.push(data[i].strstudentname);
                    scores.push(data[i].lscore);
                }
                console.log(data);

                //TODO 渲染图表
                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('dataImage'));
                myChart.showLoading({text: '正在努力的读取数据中...'  });
                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: title
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            dataView: {show: true, readOnly: false},
                            saveAsImage: {show: true}
                        }
                    },
                    tooltip: {},
                    legend: {
                        data:['分数']
                    },
                    xAxis: {
                        data: names
                    },
                    yAxis: {},
                    series: [{
                        name: '分数',
                        type: 'bar',
                        data: scores
                    }]
                };
                myChart.setOption(option);
                myChart.hideLoading();
            },error:function (error) {
                alert("数据加载失败，请重新刷新!");
            }
        });
    })


})
